package Stack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-09-02
 * Time: 19:27
 */
public class MyStack {
    //创建一个栈：基于顺序表（数组）
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[10];
    }
    //E push(E e) 将e入栈，并返回e:
    public void push(int val) {
        if (isFull()) {
            //空间不够，就扩容：
            this.elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        elem[usedSize++] = val;
    }
    public boolean isFull() {
        return this.usedSize == elem.length;
    }

    //E pop() 将栈顶元素出栈并返回:
    public int pop() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }
        int val = elem[usedSize - 1];
        usedSize--;
        return val;
    }

    //E peek() 获取栈顶元素:
    public int peek() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }
        return elem[usedSize - 1];
    }

    //boolean empty() 检测栈是否为空:
    public boolean isEmpty() {
        return usedSize == 0;
    }
}
